﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.Repositories.GPM;
using CP.Utility;
using CP.DBService;

namespace CP.Controllers.GPM
{
    public class ModuleController
    {
        public IList<Module> GetAllModules()
        {
            ModuleRepository rep = new ModuleRepository();
            return rep.GetAllModules();
        }

        public Module GetModuleByID(int id)
        {
            ModuleRepository rep = new ModuleRepository();
            return rep.GetModuleByID(id);
        }

        public bool AddModule(Module module)
        {
            ModuleRepository rep = new ModuleRepository();
            return rep.AddModule(module);
        }

        public bool EdiModule(Module module)
        {
            ModuleRepository rep = new ModuleRepository();
            return rep.UpdateModule(module);
        }

        public bool DeleteModules(IList<int> modules)
        {
            ModuleRepository rep = new ModuleRepository();
            return rep.DeleteModule(modules);
        }

        public string WebpartDraw(string modulename, string assemblyname, Dictionary<string, Dictionary<string, string>> formbtns,Dictionary<string, Dictionary<string, Dictionary<string, string>>> checkedColumns)
        {
            string existsForm = "";
            int success = 0;
            if (formbtns.Count == 0)
                throw new Exception("没有选中任何窗体节点");
            DBHelper db = DBHelper.GPMCreateInstance();
            try
            {
                db.BeginTransaction();
                ModuleRepository mRep = new ModuleRepository();
                //check whether the assembly name already exists, if exists then get module id
                //else get module id by sequence 
                Module module = mRep.GetModuleByAssemblyName(assemblyname);
                if (module.module_id == 0)
                {
                    if (string.IsNullOrEmpty(modulename))
                        throw new Exception("新增模块的模块名称不能为空");
                    module.module_id = mRep.GetModuleIDBySeq();
                    module.module_name = modulename;
                    module.assembly_name = assemblyname;
                    module.name_space = assemblyname;
                    mRep.AddModule(module);
                }
                //iterate the form list 

                WebpartRepository wRep = new WebpartRepository();
                ButtonRepository bRep = new ButtonRepository();
                ColumnListRepository cRep = new ColumnListRepository();
                foreach (KeyValuePair<string, Dictionary<string, string>> pair in formbtns)
                {
                    string formName = pair.Key.Substring(0, pair.Key.IndexOf(','));
                    string formText = "";
                    if (pair.Key.Length > (pair.Key.IndexOf(',') + 1))
                        formText = pair.Key.Substring(pair.Key.IndexOf(',') + 1);
                    //check whether the form already exists,if exists then record the exists form then continue
                    Webpart webpart = wRep.GetWebpartByFormName(formName);
                    if (webpart.webpart_id == 0)
                    {
                        //get the webpart id by sequence, insert webpart
                        webpart.webpart_id = wRep.GetWebpartIDBySeq();
                        webpart.form_name = formName;
                        webpart.module_id = module.module_id;
                        webpart.form_text = formText;
                        wRep.AddWebpart(webpart);

                        //iterate form's button list
                        foreach (KeyValuePair<string, string> btnPair in pair.Value)
                        {
                            Button_List btn = new Button_List();
                            btn.action_id = btnPair.Key;
                            btn.button_name = btnPair.Value;
                            bRep.AddButton(webpart.webpart_id, btn);
                        }

                        Dictionary<string, Dictionary<string, string>> gridColumns = new Dictionary<string,Dictionary<string,string>>();
                        if (checkedColumns.TryGetValue(formName, out gridColumns))
                        {
                            foreach (KeyValuePair<string, Dictionary<string, string>> grid in gridColumns)
                            {
                                string gridName = grid.Key;
                                foreach (KeyValuePair<string, string> col in grid.Value)
                                {
                                    Column_List columnList = new Column_List();
                                    columnList.list_name = col.Value;
                                    columnList.field_name = col.Key;
                                    columnList.grid_name = gridName;
                                    columnList.webpart_id = webpart.webpart_id;
                                    cRep.AddColumn_List(webpart.webpart_id, columnList);
                                }
                            }
                        }
                        success++;
                    }
                    else
                    {
                        existsForm = existsForm == "" ? formName : (existsForm + "," + formName);
                        continue;
                    }
                }
                db.Commit();
            }
            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
            return "共选中【" + formbtns.Count + "】个窗体；" + "成功提取窗体数【" + success + "】" + "；提取失败已经存在的窗体:" + existsForm;
        }
    }
}
